import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/home/widgets/run_message_widgets.dart';
import 'package:dmz/business/mine/model/news_model.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:dmz/routers/routers.dart';
import 'package:dmz/utils/customize_loding.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/get_rx.dart';

class NewsCenterController extends BaseController {
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  RxInt selectIndex = 0.obs;
  PageController pageController = PageController();
  List<List<NewsModel>> alldatas = [[], [], []];
  List<String> timesStr = [];
  NewsModel? selectModel;

  @override
  void onInit() {
    String dateStr = SPUtils.getTimeStr();
    timesStr = [dateStr, dateStr, dateStr];
    getAcceptShareList(0, 1);
    super.onInit();
  }

  didAllNewsReaded(int tag) {
    RequestClient().post<NewsPageData>(APIS.messageNotify,
        data: {'message_type': tag + 1, 'flag': 1}).then((value) {
      for (var element in alldatas[tag]) {
        element.flag = 1;
      }
      update(['news$tag']);
    });
  }

  didNewsReaded(int id, int tag) {
    RequestClient().post<NewsPageData>(APIS.messageNotify,
        data: {'id': id, 'flag': 1}).then((value) {
      for (var element in alldatas[tag]) {
        element.flag = 1;
      }
      update(['news$tag']);
    });
  }

  changeSelectPage(int index) {
    selectIndex.value = index;
    pageController.jumpToPage(index);
    getAcceptShareList(index, 1);
  }

  Future<void> getAcceptShareList(int tag, int index) async {
    var data =
        await RequestClient().post<NewsPageData>(APIS.messageNotify, data: {
      'indx': index,
      'size': 20,
      'message_type': tag + 1,
      'time_stamp': SPUtils.getTimeInt(timesStr[tag])
    });

    index == 1
        ? alldatas[tag] = data?.list ?? []
        : alldatas[tag].addAll(data?.list ?? []);

    update(['news$tag']);
  }

  ///日期选择
  selectDate(int tag) {
    EMILoding.showBottomWindow(
        RunMessageWidgets.seleDateWheelWidget(timesStr[tag], (value) {
          ///时间选择完成
          timesStr[tag] = value;
          Get.back();
          getAcceptShareList(tag, 0);
        }),
        isDismissible: true,
        showBack: true);
  }

  ///消息点击 详情
  clickNews(NewsModel data, int tag) {
    selectModel = data;
    data.flag = 1;
    Get.toNamed(RouteGet.newsDetails);
    update(['news$tag']);
    didNewsReaded(data.id ?? 0, tag);
  }
}
